package com.mohe.dangjian.action;

import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;

import com.mohe.dangjian.model.Admin;
import com.mohe.dangjian.model.Role;
import com.mohe.dangjian.service.interfaces.IAdmin;
import com.mohe.dangjian.service.interfaces.IRole;
import com.mohe.dangjian.utils.Json;
import com.mohe.dangjian.utils.JsonUtil;
import com.mohe.dangjian.utils.MD5Util;
import com.mohe.dangjian.utils.Page;
import com.opensymphony.xwork2.ActionSupport;

public class AdminAction extends ActionSupport {

	private static final long serialVersionUID = 3441001507045999014L;

	private IAdmin adminServ;
	private IRole roleServ;
	
	private List<Admin> adminList;
	private List<Role> roleList;
	private Admin admin = new Admin();
	private Page page = new Page();
	
	// 页面消息
	private String flag;

	public IAdmin getAdminServ() {
		return adminServ;
	}

	public void setAdminServ(IAdmin adminServ) {
		this.adminServ = adminServ;
	}

	public List<Admin> getAdminList() {
		return adminList;
	}

	public void setAdminList(List<Admin> adminList) {
		this.adminList = adminList;
	}

	public Admin getAdmin() {
		return admin;
	}

	public void setAdmin(Admin admin) {
		this.admin = admin;
	}

	public Page getPage() {
		return page;
	}

	public void setPage(Page page) {
		this.page = page;
	}

	public String getFlag() {
		return flag;
	}

	public void setFlag(String flag) {
		this.flag = flag;
	}
	
	public IRole getRoleServ() {
		return roleServ;
	}

	public void setRoleServ(IRole roleServ) {
		this.roleServ = roleServ;
	}

	public List<Role> getRoleList() {
		return roleList;
	}

	public void setRoleList(List<Role> roleList) {
		this.roleList = roleList;
	}

	public String getAdminListForPage(){
		int count = adminServ.getCount(admin);
		page.setCount(count);
		adminList = adminServ.getAdminListForPage(admin, page);
		return "admin.jsp";
	}
	
	/**
	 * 分页
	 */
	public void searchAdminList() {
		Json json = new Json();
		List<Map<String, String>> mapList = new ArrayList<Map<String, String>>();
		int count = adminServ.getCount(admin);
		page.setCount(count);
		List<Admin> list = adminServ.getAdminListForPage(admin, page);
		if (list != null) {
			int index = (page.getCurrPage() - 1) * page.getPageSize() + 1;
			for (Admin s : list) {
				Map<String, String> map = new HashMap<String, String>();
				map.put("index", index + "");
				if (s.getTimeKey() != null) {
					map.put("timeKey", s.getTimeKey() + "");
				} else {
					map.put("timeKey", "0");
				}
				if (!StringUtils.isBlank(s.getName())) {
					map.put("name", s.getName());
				} else {
					map.put("name", "");
				}
				if (s.getRole() != null && !StringUtils.isBlank(s.getRole().getName())) {
					map.put("roleName", s.getRole().getName());
				} else {
					map.put("roleName", "");
				}
				mapList.add(map);
				index++;
			}
		}
		json.setObject(page);
		json.setMap(mapList);
		JsonUtil.writeJson(json);
	}
	
	/**
	 * 跳转到编辑界面
	 * @return
	 */
	public String toEditAdmin(){
		admin = adminServ.getAdminById(admin.getTimeKey());
		roleList = roleServ.getAllRole();
		return "editAdmin.jsp";
	}
	
	/**
	 * 更新管理员信息
	 * @throws NoSuchAlgorithmException 
	 */
	public void updateAdmin() throws NoSuchAlgorithmException{
		Json json = new Json();
		// 判断是否存在该用户名的管理员
		Admin isAdmin = adminServ.getAdminByName(admin.getName());
		if(isAdmin != null){ // 存在
			json.setSuccess(false);
			json.setMessage("编辑失败,已存在相同的管理员名称");
		}else{ // 不存在
			Admin newAdmin = new Admin();
			newAdmin.setTimeKey(admin.getTimeKey());
			newAdmin.setName(admin.getName());
			newAdmin.setPassword(MD5Util.encrypt(admin.getPassword()));
			Role role = roleServ.getRoleById(admin.getRole().getTimeKey());
			if(role!=null){
				newAdmin.setRole(role);
				boolean result = adminServ.insert(newAdmin);
				if(result){
					json.setMessage("编辑成功");
				}else{
					json.setMessage("编辑失败");
					json.setSuccess(false);
				}
			}else{
				json.setMessage("编辑失败");
				json.setSuccess(false);
			}
		}
		
		JsonUtil.writeJson(json);
	}
	
	/**
	 * 跳转到新增界面
	 * @return
	 */
	public String toAddAdmin(){
		roleList = roleServ.getAllRole();
		return "addAdmin.jsp";
	}
	
	/**
	 * 保存新增管理员
	 * @throws NoSuchAlgorithmException 
	 */
	public void saveAdmin() throws NoSuchAlgorithmException {
		Json json = new Json();
		// 判断是否存在该用户名的管理员
		Admin isAdmin = adminServ.getAdminByName(admin.getName());
		if(isAdmin != null){ // 存在
			json.setSuccess(false);
			json.setMessage("新增失败,已存在相同的管理员名称");
		}else{ // 不存在
			Admin newAdmin = new Admin();
			newAdmin.setName(admin.getName());
			newAdmin.setPassword(MD5Util.encrypt(admin.getPassword()));
			Role role = roleServ.getRoleById(admin.getRole().getTimeKey());
			if(role!=null){
				newAdmin.setRole(role);
				boolean result = adminServ.insert(newAdmin);
				if(result){
					json.setMessage("新增成功");
				}else{
					json.setMessage("新增失败");
					json.setSuccess(false);
				}
			}else{
				json.setMessage("新增失败");
				json.setSuccess(false);
			}
		}
		
		JsonUtil.writeJson(json);
	}
	
	/**
	 * 删除管理员
	 */
	public void delAdmin() {
		Json json = new Json();
		boolean result = adminServ.deleteAdmin(admin.getTimeKey());
		if (result) {
			json.setMessage("删除成功");
		} else {
			json.setSuccess(false);
			json.setMessage("删除失败");
		}
		JsonUtil.writeJson(json);
	}
}
